package com.mlethe.library.fingerprint;

import android.content.Context;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.mlethe.library.fingerprint.callback.FingerprintCallback;
import com.mlethe.library.fingerprint.callback.IFingerprint;
import com.mlethe.library.fingerprint.entity.VerificationDialogStyle;

/**
 * Android M == 6.0
 * Created by Mlethe on 2019/7/9.
 */
@RequiresApi(api = Build.VERSION_CODES.M)
class FingerprintImplForAndrM implements IFingerprint {

    /**
     * 指纹验证框
     */
    private FingerprintDialog fingerprintDialog;
    /**
     * 指向调用者的指纹回调
     */
    private FingerprintCallback fingerprintCallback;

    private FingerprintImplForNoDialog fingerprintImplForNoDialog;

    /**
     * 弹窗样式
     */
    private VerificationDialogStyle verificationDialogStyle;

    private static final class Holder {
        private static final FingerprintImplForAndrM INSTANCE = new FingerprintImplForAndrM();
    }

    private FingerprintImplForAndrM() {
    }

    public static FingerprintImplForAndrM getInstance() {
        FingerprintImplForAndrM instance = Holder.INSTANCE;
        instance.fingerprintImplForNoDialog = FingerprintImplForNoDialog.getInstance();
        return instance;
    }

    /**
     * 指纹验证框按键监听
     */
    private FingerprintDialog.OnDialogActionListener dialogActionListener = new FingerprintDialog.OnDialogActionListener() {

        @Override
        public void onCancel() {
            // 取消指纹验证
            if (fingerprintImplForNoDialog != null) {
                fingerprintImplForNoDialog.cancel();
            }
        }
    };

    /**
     * 调起指纹验证
     *
     * @param context
     */
    @Override
    public void authenticate(Context context) {
        // 指纹验证框
        fingerprintDialog = new FingerprintDialog(context, verificationDialogStyle).setActionListener(dialogActionListener);
        fingerprintDialog.show();
        fingerprintImplForNoDialog.setFingerprintCallback(new FingerprintCallback() {
            @Override
            public void onHwUnavailable() {
                if (fingerprintCallback != null) {
                    fingerprintCallback.onHwUnavailable();
                }
                onDestroy();
            }

            @Override
            public void onNoneEnrolled() {
                if (fingerprintCallback != null) {
                    fingerprintCallback.onNoneEnrolled();
                }
                onDestroy();
            }

            @Override
            public void onSucceeded() {
                if (fingerprintCallback != null) {
                    fingerprintCallback.onSucceeded();
                }
                onDestroy();
            }

            @Override
            public void onFailed() {
                if (fingerprintDialog != null) {
                    fingerprintDialog.setTip(R.string.biometricprompt_verify_failed, R.color.biometricprompt_color_FF5555);
                }
                if (fingerprintCallback != null) {
                    fingerprintCallback.onFailed();
                }
            }

            @Override
            public void onError(String msg) {
                if (fingerprintDialog != null) {
                    fingerprintDialog.setTip(msg, R.color.biometricprompt_color_FF5555);
                }
                if (fingerprintCallback != null) {
                    fingerprintCallback.onError(msg);
                }
            }

            @Override
            public void onCancel() {
                if (fingerprintCallback != null) {
                    fingerprintCallback.onCancel();
                }
                onDestroy();
            }
        });
        fingerprintImplForNoDialog.authenticate(context);
    }

    /**
     * 设置回调监听
     *
     * @param callback
     */
    @Override
    public void setFingerprintCallback(FingerprintCallback callback) {
        fingerprintCallback = callback;
    }

    /**
     * 设置弹窗样式
     *
     * @param verificationDialogStyle
     */
    @Override
    public void setVerificationDialogStyle(VerificationDialogStyle verificationDialogStyle) {
        this.verificationDialogStyle = verificationDialogStyle;
    }

    /**
     * 在 Android Q，Google 提供了 Api BiometricManager.canAuthenticate() 用来检测指纹识别硬件是否可用及是否添加指纹
     * 不过尚未开放，标记为"Stub"(存根)
     * 所以暂时还是需要使用 Andorid 6.0 的 Api 进行判断
     */
    @Override
    public boolean canAuthenticate(Context context) {
        return fingerprintImplForNoDialog.canAuthenticate(context);
    }

    @Override
    public void cancel() {

    }

    @Override
    public void onDestroy() {
        if (fingerprintDialog != null) {
            fingerprintDialog.cancel();
            fingerprintDialog = null;
        }
        fingerprintCallback = null;
    }

}
